package com.futvan.z.erp.erp_search_engines.job;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import com.futvan.z.erp.erp_search_engines_info.erp_search_engines_info;
import com.futvan.z.framework.common.bean.z_form_table;
import com.futvan.z.framework.core.SuperService;
import com.futvan.z.framework.core.z;
import com.futvan.z.framework.util.DateUtil;
import com.futvan.z.framework.util.EmailUtil;
import com.futvan.z.framework.util.HttpUtil;
import com.futvan.z.framework.util.SpringUtil;
import com.futvan.z.framework.util.StringUtil;

public class job_baidu_m implements Job {

	public void execute(JobExecutionContext context) throws JobExecutionException {
		//关键字
		String keyinfo = String.valueOf(context.getJobDetail().getJobDataMap().get("keyinfo"));
		//模板ID
		String zid = String.valueOf(context.getJobDetail().getJobDataMap().get("zid"));
		//执行采集任务
		try {
			getList(keyinfo,10,null,0,zid);
		} catch (Exception e) {
			throw new JobExecutionException("采购百度PC端舆情信息出错|"+e.getMessage());
		}
	}

	public static void main(String[] args) throws Exception {
		//关键字
		String keyinfo = "美罗国际";
		//模板ID
		String zid = "999";
		//执行采集任务
		try {
			getList(keyinfo,10,null,0,zid);
		} catch (Exception e) {
			throw new JobExecutionException("采购百度PC端舆情信息出错|"+e.getMessage());
		}
	}

	/**
	 * 采集在百度的舆情信息
	 * @param search 关键词
	 * @param pages 采集百度页数
	 * @param ip 代理IP
	 * @param port 代理IP端口
	 * @param email 接收报告Email
	 * @param zid 舆情模板ID
	 * @return
	 * @throws Exception
	 */
	public static void getList(String search,int pages,String ip,int port, String zid) throws Exception {
		if(pages<1) {
			pages = 1;
		}
		search = StringUtil.UrlEncode(search);
		if(z.isNotNull(search)) {
			for (int i = 0; i < pages; i++) {
				//SystemUtil.Sleep(10000);

				int page_num = i+1;
				String url = "https://www.baidu.com/s?ie=utf-8&usm=8&sa=np&rqid=10225023202234102767";
				//添加关键字
				url = url + "&word="+search;
				//添加分页
				if(page_num>1) {
					url = url +"&pn="+i*10;
				}

				String html = "";
				if(z.isNotNull(ip) && port>0) {
					html = HttpUtil.doGetThrowsException(url, null,ip,port);
				}else {
					html = HttpUtil.doGet(url, null);
				}

				Document doc = Jsoup.parse(html);
				Elements list = doc.select("#content_left div");
				for (Element el : list) {
					String cms_title = el.select("h3").text();//标题
					//判读数据库中是否存在
//					int num = z.getSqlSession().selectOne("selectoneint","SELECT COUNT(*) FROM erp_search_engines_info WHERE title = '"+cms_title+"' AND search_engines_id = '"+zid+"'");
//					if(num==0) {
						String from_website = el.select(".f13 a.c-showurl").text();//来源网站地址
						String from_url = el.select("h3 a").attr("href");//URL
						if(z.isNotNull(from_url)) {

							String ipinfo = "";
							if(z.isNotNull(ip) && port>0) {
								ipinfo = "|代理IP："+ip+"|端口："+port;
							}
							z.Log("【第"+page_num+"页】"+ipinfo+"|标题："+cms_title+"|来源："+from_website);

							//保存网站信息
//							erp_search_engines_info info = new erp_search_engines_info();
//							info.setZid(z.newZid("erp_search_engines_info"));
//							info.setTitle(cms_title);
//							info.setFrom_website(from_website);
//							info.setUrl(from_url);
//							info.setSseq("出现在第"+page_num+"页");
//							info.setSearch_engines_id(zid);
//							z.getSqlSession().insert("erp_search_engines_info_insert", info);
						}
//					}
				}

				if(list.size()==0) {
					z.Log(html);
				}
			}
		}else {
			z.Error("关键词无法转换成UrlEncode");
		}
	}
}
